package com.xbongbong.crm.service.permission;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.util.ElasticSearchConditionUtil;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.abstracts.AbstractBusinessPermissionService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.permission.pojo.dto.VerifyViewPermissionDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.util.UserPermissionUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 吴峰
 * @date 2022/05/13 10:34
 */
@Service("contractPermissionServiceImpl")
@Slf4j
public class ContractPermissionServiceImpl extends AbstractBusinessPermissionService<PaasFormDataEntityExt, PaasFormEntityExt> {



    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode(), XbbRefTypeEnum.ORDER.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {
        VerifyViewPermissionDTO verifyViewPermissionDTO = new VerifyViewPermissionDTO(loginUser, paasFormDataEntityExt);
        //没有团队权限且没有共享抛异常
        boolean hasDataPermission = hasPermission(formEntity, paasFormDataEntityExt, loginUser, businessType)
                || sharePermissionHelp.verifySharePermission4QueryWithRuleEngine(verifyViewPermissionDTO);
        if (!hasDataPermission) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
    }




    @Override
    public Boolean checkPermissionByTeam(String corpid, Integer businessType, BoolQueryBuilder boolQueryBuilder) throws XbbException {
        return checkPermissionByTeam(boolQueryBuilder, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
    }

    @Override
    public boolean hasPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {
        if (UserPermissionUtil.isMaxDataPermission(loginUser, formEntity.getSaasMark(), formEntity.getBusinessType(), formEntity.getDistributorMark(), formEntity.getMenuId(), false)){
            return true;
        }
        String corpid = paasFormDataEntityExt.getCorpid();
        Long dataId = paasFormDataEntityExt.getId();
        List<String> userIdIn = getUserIdIn(loginUser);
        BoolQueryBuilder boolQueryBuilder = ElasticSearchConditionUtil.permissionQuery(dataId, userIdIn, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
        return checkPermissionByTeam(paasFormDataEntityExt.getCorpid(), businessType, boolQueryBuilder);
    }
}
